package com.nmgydy.admin.controller;

import com.alibaba.fastjson.JSON;
import com.nmgydy.beans.ListBean;
import com.nmgydy.beans.Message;
import com.nmgydy.beans.ResultBean;
import com.nmgydy.model.BpmnTask;
import com.nmgydy.util.Functions;
import com.nmgydy.util.HibernateDao;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/node")
public class NodeController {
	@Autowired
	private HibernateDao hibernateDao;

	@RequestMapping("list")
	public String list(){
		return "/admin/node/list";
	}
	@RequestMapping("add")
	public String add(){
		return "/admin/node/add";
	}
	@RequestMapping("edit")
	public String edit(){
		return "/admin/node/edit";
	}
	@RequestMapping("preview")
	public String preview(String type,Model model){
		model.addAttribute("type",type);
		return "/admin/node/preview";
	}

	@ResponseBody
	@RequestMapping("listData")
	public String listData(BpmnTask vo, HttpServletRequest request) throws Exception{
		String pageSize = request.getParameter("pageSize");
		String pageIndex = request.getParameter("pageIndex");
		String sortField = request.getParameter("sortField");
		String sortOrder = request.getParameter("sortOrder");
		String name = request.getParameter("name");
		String hql = "from BpmnTask where id=id";
		Map<String,Object> params = new HashMap<String,Object>();
		if (!Functions.isEmpty(name)) {
			hql += " and name like :name";
			params.put("name", "%"+name+"%");
		}
		String caseType = request.getParameter("caseType");
		if (!Functions.isEmpty(caseType)) {
			hql += " and caseType = :caseType";
			params.put("caseType", caseType);
		}
		if(!Functions.isEmpty(sortField)&&!Functions.isEmpty(sortOrder)){
			hql += " order by " + sortField + " " + sortOrder;
		}
		@SuppressWarnings("unchecked")
		List<BpmnTask> nodeList = (List<BpmnTask>) hibernateDao.list(hql, params, Integer.parseInt(pageIndex)+1, Integer.parseInt(pageSize));
		int total = (int) hibernateDao.count(hql, params);
		return JSON.toJSONString(new ListBean(nodeList,total));
	}

	@ResponseBody
	@RequestMapping("save")
	public String save(BpmnTask record) throws Exception{
		boolean flag = hibernateDao.save(record);
		Message message=new Message();
		if(flag){
			message.setRes(true);
			message.setMsg("添加成功");
		}else{
			message.setRes(false);
			message.setMsg("添加失败");
		}
		return JSON.toJSONString(message);
	}

	@ResponseBody
	@RequestMapping("update")
	public String update(BpmnTask record) throws Exception{
		if(record.getId()==null){
			return JSON.toJSONString(new Message(false,"id is null"));
		}
		if(record.getDays()==null){
			Logger.getLogger(NodeController.class).error("days is null");
		}
		String hql = "update BpmnTask set days=:days where id=:id";
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("days",record.getDays());
		params.put("id",record.getId());
		hibernateDao.executeUpdate(hql,params);
		return JSON.toJSONString(ResultBean.success("success"));
	}

	@ResponseBody
	@RequestMapping("remove")
	public String delete(Integer id) throws Exception{
		if(id == null){
			Logger.getLogger(NodeController.class).error("ID is null");
			return JSON.toJSONString(new Message(false,"删除失败"));
		}
		Message message = new Message();
		BpmnTask node = hibernateDao.load(BpmnTask.class, id);
		boolean flag = hibernateDao.delete(node);
		if (flag) {
			message.setRes(true);
			message.setMsg("删除成功");
		} else {
			message.setRes(false);
			message.setMsg("删除失败");
		}

		return JSON.toJSONString(message);
	}

}
